package com.deere.manufacture.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.deere.action.BaseAction;
import com.deere.common.Utils;
import com.deere.exception.GenericException;
import com.deere.manufacture.service.WorkOrderService;
import com.deere.model.ProductionOrderItem;
import com.deere.model.Scrap;
import com.deere.model.WorkOrder;
import com.deere.model.dto.ScrapDto;
import com.deere.scrap.service.ScrapService;
import com.deere.workprocedure.service.ItemWorkflowService;

/**
 * 工作单相关的业务方法
 * 
 * @author zhaohongxing
 * 
 */
public class WorkOrderAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2144382099128552441L;

	private String dataJson;
	/*
	 * 用于查询
	 */
	private WorkOrder workOrder;
	/*
	 * 用于页面展现，最终会自动转换成json格式。
	 */
	private List<WorkOrder> workOrders;
	/*
	 * 用于接收工作单修改页面提交的数据
	 */
	private String workerName;
	/*
	 * 
	 */
	private String id;
	/*
	 * 
	 */
	private Integer itemId;

	private List<Scrap> scraps;
	
	private ScrapDto scrapDto;

	@Autowired
	private WorkOrderService workOrderService;

	@Autowired
	private ItemWorkflowService iwService;

	@Autowired
	private ScrapService scrapService;

	/**
	 * 显示所有的生产计划单
	 * 
	 * @return
	 */
	public String listProductionPlan() {
		workOrderService.listProductionPlan();
		return "listProductionPlan";
	}

	/**
	 * 根据<code>itemId</code>检索对应的工作单
	 * 
	 * @return
	 * @throws Exception
	 * @see #itemId
	 */
	public String queryWorkOrderseByItemId() throws Exception {
		workOrders = workOrderService.getPlannedProductionItem(itemId);
		return "queryWorkOrderseByItemId";
	}

	/**
	 * 添加流水记录（工作单）。
	 * 
	 * @return
	 * @throws GenericException
	 * @see {@link #workOrder}
	 */
	public String insertWorkOrder() throws GenericException {
		List<WorkOrder> workOrders = Utils.json2Object(dataJson, WorkOrder.class);
		List<ProductionOrderItem> orderItems = Utils.json2Object(dataJson, ProductionOrderItem.class);
		for (int i = 0; i < workOrders.size(); i++) {
			WorkOrder workOrder = workOrders.get(i);
			workOrder.setProductionOrderItem(orderItems.get(i));
		}
		workOrderService.insertWorkOrder(workOrders);
		return "listProductionPlan";
	}

	/**
	 * 更新工作单。 主要用来完成工作单的细节工作，如填报：实际完成数量、首检员、检验员等
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String saveWorkOrder() throws GenericException {
		workOrderService.updateWorkOrder(workOrder);
		return "editSuccess";
	}

	/**
	 * 保存分配的工作单。
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String saveAssignWorkOrder() throws GenericException {
		workOrderService.assignWorkOrder(workOrder);
		return "assigned";
	}

	/**
	 * 创建新的工作单
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String createNewWorkOrder() throws GenericException {
		workOrderService.updateWorkOrder(workOrder);
		return listWorkOrder();
	}

	/**
	 * 查询所有状态为NEW的工作单。
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String listWorkOrder() throws GenericException {
		workOrders = workOrderService.getCanReportWorkOrders(workOrder);
		return "listWorkOrder";
	}

	/**
	 * 进入工作单上报页面
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String toReport() throws GenericException {
		workOrder = workOrderService.loadWorkOrder(Integer.valueOf(id));
		return "toReport";
	}

	/**
	 * 进入工作单报废页面
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String toScrap() throws GenericException {
		workOrder = workOrderService.loadWorkOrder(workOrder.getId());
		return "toScrap";
	}

	/**
	 * 报废流程处理
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String scrap() throws GenericException {
		workOrderService.scrap(workOrder);
		return "editSuccess";
	}

	/**
	 * 查询报废的零件
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String scrapIndex() throws GenericException {
		// workOrderService.scrap(workOrder);
		return "scrapIndex";
	}

	/**
	 * 查询报废的零件
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String searchScrap() throws GenericException {
		scraps = scrapService.searchScrap(scrapDto);
		return "searchScrap";
	}

	/**
	 * 工作单上报
	 * 
	 * @return
	 * @throws GenericException
	 */
	public String report() throws GenericException {
		workOrderService.updateWorkOrder(workOrder);
		return "unReportWorkOrders";
	}

	public String execute() throws Exception {
		return SUCCESS;
	}

	/**
	 * 进入编辑工作单的方法
	 * 
	 * @return
	 * @throws Exception
	 */
	public String unReportWorkOrders() throws Exception {
		return "unReportWorkOrders";
	}

	/**
	 * 初始化编辑工作单页面
	 * 
	 * @return
	 */
	public String initWorkOrder() {
		workOrder = workOrderService.loadWorkOrder(Integer.valueOf(id));
		return "initWorkOrder";
	}

	/**
	 * 初始化分配工作单页面
	 * 
	 * @return
	 */
	public String assignWorkOrder() {
		workOrder = workOrderService.loadWorkOrder(Integer.valueOf(id));
		return "assignWorkOrder";
	}

	/** ******************getter and setter method********************************** */

	public String getDataJson() {
		return dataJson;
	}

	public WorkOrder getWorkOrder() {
		return workOrder;
	}

	public void setWorkOrder(WorkOrder workOrder) {
		this.workOrder = workOrder;
	}

	public List<WorkOrder> getWorkOrders() {
		return workOrders;
	}

	public void setWorkOrders(List<WorkOrder> workOrders) {
		this.workOrders = workOrders;
	}

	public String getWorkerName() {
		return workerName;
	}

	public void setWorkerName(String workerName) {
		this.workerName = workerName;
	}

	public void setDataJson(String dataJson) {
		this.dataJson = dataJson;
	}

	public Integer getItemId() {
		return itemId;
	}

	public void setItemId(Integer itemId) {
		this.itemId = itemId;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Scrap> getScraps() {
		return scraps;
	}

	public void setScraps(List<Scrap> scraps) {
		this.scraps = scraps;
	}

	public void setScrapDto(ScrapDto scrapDto) {
		this.scrapDto = scrapDto;
	}

	public ScrapDto getScrapDto() {
		return scrapDto;
	}
}
